//获取应用实例
const app = getApp()
const request = require('../../../utils/request');
//获取当前页面
Page({
  //初始化数据
  data: {
      payPort:'web',       //支付端口
      payWay:'wechat',     //支付方式
      payword:'',          //支付密码
      orderId: '',         //订单ID
      money:'',            //订单金额
      payArlet:false,      //监听余额支付框
      isFocus: false,
      wallets_password:0,
      type:'',
      inputVal: ''
  },
  //获取url参数
  onLoad: function(e){
    console.log(e)
    let price = Math.floor(e.price * 100)/100
    this.setData({
      orderId:e.order_id,
      money:price,
      type: e.type
    })
  },
  //单选按钮组
  radioChange: function (e) {
    this.setData({
      payWay: e.detail.value
    })
  },
  //页面初始化
  onReady: function () {
    var that = this
    console.log(that.data)
    //页面标题
    wx.setNavigationBarTitle({
      title: '订单支付'
    })
  },

  //获取钱包密码
  set_wallets_password(e) {
    this.setData({
      wallets_password: e.detail.value
    });
    console.log(this.data.wallets_password);
    if (this.data.wallets_password.length == 6) {
      wx.showToast({
        title: '正在支付',
        icon: 'loading',
        duration: 2000,
        success: () => {
          //调起起支付
          this.paybalance()
        }
      })
    }
  },

  //开始支付
  pay:function(){
      var that = this
      //选择支付方式调起支付
      if(that.data.payWay=='zhanglitong'){
        that.setData({
          payArlet: true
        })
      }else{
          //调起支付接口
          request.HttpRequst(true,'pay',2,'',{"pay_way":that.data.payWay, 'order_id':that.data.orderId, 'pay_port':'miniapp','payword':wx.getStorageSync("sessionId")},"POST",false,res => {
              console.log(res);
              if( res.code == '200') {
                  wx.requestPayment({
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr,
                      'package': res.data.package,
                      'signType': 'MD5',
                      'paySign': res.data.paySign,
                      'success':function(res){
                          console.log("支付成功")
                          that.close_wallets_password();
                          try {
                            wx.removeStorageSync('foods');
                            if(that.data.type == '预定'){
                              wx.redirectTo({
                                url: '../../../pagesA/invitation/sendInvitation/sendInvitation?order_id='+that.data.orderId
                              })
                            }else{
                              wx.redirectTo({
                                url: '../../my/order/order'
                              })
                            }
                          } catch (e) {
                            // Do something when catch error
                          }
                          that.setData({
                            inputVal: ''
                          })
                      },
                      'fail':function(res){
                          console.log("支付失败")
                          that.close_wallets_password()
                          that.setData({
                            inputVal: ''
                          })
                      }
                  })
              } 
          })
      }
      console.log(that.data)
  },

  //余额支付
  paybalance() {
    let that = this;
    //调起支付接口
    request.HttpRequst(true,'pay',2,'',{"pay_way":that.data.payWay, 'order_id':that.data.orderId, 'pay_port':'web', 'payword':that.data.wallets_password,'token': wx.getStorageSync("sessionId")},"POST",false,res => {
        console.log(res);
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: res.info,
            showCancel: false,
            success: function (res) {
              that.close_wallets_password();
              try {
                wx.removeStorageSync('foods');
                if(that.data.type == '预定'){
                  wx.redirectTo({
                    url: '../../../pagesA/invitation/sendInvitation/sendInvitation?order_id='+that.data.orderId
                  })
                }else{
                  wx.redirectTo({
                    url: '../../my/order/order'
                  })
                }
              } catch (e) {
                // Do something when catch error
              }
            }
          })
        }else{
          wx.showModal({
            title: '支付失败',
            content: res.info,
            showCancel: false,
            success: function (res) {
              that.close_wallets_password()
            }
          })
        }
    })
  },

  //关闭
  close_wallets_password() {
    this.setData({
      payArlet: false,
      isFocus: false,//失去焦点
    })
  },

  //聚焦input
  set_Focus() {
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },

   
  
})